<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const pepper = {
        uname: '佩奇',
        age: 3,
        action: ['eat', 'drink'],
        family: {
          uname: '猪爸',
          age: 5,
          sing: ['唱', '跳'],
        },
      }
      // 展开运算符方法进行浅拷贝
      // const pig = { ...pepper }
      // console.log(pig)
      // pig.uname = '猪猪'
      // console.log(pig)
      // console.log(pepper);

      // assign方法进行浅拷贝
      // const pig = {}
      // Object.assign = (pig, pepper)
      // pig.uname = '猪猪'
      // console.log(pig)
      // console.log(pepper)

      // for in 方法进行浅拷贝
      // const pig = {}
      // for (let key in pepper) {
      //   pig[key] = pepper[key]
      // }
      // console.log(pig)

      // 递归
      // let i = 0
      // function fn() {
      //   if (i >= 6) {
      //     console.log(i)
      //     return
      //   }
      //   i++
      //   fn()
      // }
      // fn()

      const pig = {}
      function deepClone(newObj, oldObj) {
        for (let key in oldObj) {
          if (oldObj[key] instanceof Array) {
            newObj[key] = []
            deepClone(newObj[key], oldObj[key])
          } else if (oldObj[key] instanceof Object) {
            newObj[key] = {}
            deepClone(newObj[key], oldObj[key])
          } else {
            newObj[key] = oldObj[key]
          }
        }
      }
      deepClone(pig, pepper)
      pig.uname = '猪猪'
      pig.action = '叫'
      pig.family.age = 4
      console.log(pig)
      console.log(pepper)
    </script>
  </body>
</html>
